package com.ruoyi.web.controller.oto;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.oto.constants.DeleteFlag;
import com.ruoyi.oto.domain.BranchLink;
import com.ruoyi.oto.domain.BranchMember;
import com.ruoyi.oto.domain.CompanyCore;
import com.ruoyi.oto.domain.dto.BindCompanyDTO;
import com.ruoyi.oto.domain.dto.BranchMembQuryDTO;
import com.ruoyi.oto.domain.vo.BaseCompanyVO;
import com.ruoyi.oto.service.BranchMemberService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台-企联用户控制器
 */
@RestController
@RequestMapping("/adm/branchMeb")
public class BranchMemberController extends BaseController{

    @Autowired
    private BranchMemberService branchMemberService;

    /**
     * 分页获取企联用户  OK PAGED
     * @param quryDTO
     * @return
     */
    @PostMapping("/page")
    public AjaxResult<IPage<BranchMember>> pageBranchMeb(@RequestBody BranchMembQuryDTO quryDTO) {
        BranchMember branchMember = new BranchMember();
        IPage<BranchMember> page = new Page<>(quryDTO.getCurrent(), quryDTO.getSize());
        LambdaQueryWrapper<BranchMember> wrapper = new LambdaQueryWrapper<BranchMember>();
        if(quryDTO.getBind()!=null&&quryDTO.getBind()){
            wrapper.isNotNull(BranchMember::getBindTime);
        }
        else if(quryDTO.getBind()!=null && !quryDTO.getBind()){
            wrapper.isNull(BranchMember::getBindTime);
        }
        wrapper.eq(StrUtil.isNotEmpty(quryDTO.getMemberLinkName()), BranchMember::getMemberLinkName, quryDTO.getMemberLinkName());
        IPage<BranchMember> result = branchMember.selectPage(page,wrapper);
        return AjaxResult.success(result);
    }

    /**
     * 绑定解绑联企人员和企业的关系  OK  PAGED
     * @param bindCompanyDTO
     * @return
     */
    @PostMapping("linkCompany")
    public AjaxResult<Long> bindCompany(@RequestBody @Validated BindCompanyDTO bindCompanyDTO){
        if(bindCompanyDTO.getCompanyIds()!=null && bindCompanyDTO.getCompanyIds().size()>0){
            branchMemberService.linkCompany(bindCompanyDTO,this.getLoginUser());
            return AjaxResult.success(1L);
        }
        return AjaxResult.error("参数有误");
    }

    /**
     * 获取当前联企用户管理的企业 OK PAGED
     * @return
     */
    @PostMapping("getCompanys/{memberLinkId}")
    public AjaxResult<List<BaseCompanyVO>> getCompanys(@PathVariable Long memberLinkId){
        BranchLink branchLink = new BranchLink();
        List<BranchLink> branchLinks = branchLink.selectList(new LambdaQueryWrapper<BranchLink>().eq(BranchLink::getMemberLinkId,memberLinkId).eq(BranchLink::getDelFlag, DeleteFlag.NO));
        List<BaseCompanyVO> companys = new ArrayList<>();
        for(BranchLink branchLink1:branchLinks){
            companys.add(new BaseCompanyVO(branchLink1.getCompanyId(),branchLink1.getCompanyName()));
        }
        return AjaxResult.success(companys);


    }
}
